package com.data.hive.service.impl;

import com.data.hive.dao.TableInfo;
import com.data.hive.service.MetaService;
import com.data.hive.utils.HiveMetaStoreSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
import org.apache.thrift.TException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * hive meta
 *
 * @author andy
 * @version 1.0
 * @date 2022-01-17 10:01 上午
 */
@Service
@Slf4j
public class MetaServiceImpl implements MetaService {

    public List<TableInfo> allTBls() {
        List<TableInfo> tbls = new ArrayList<>();
        try {
            //设置hiveMetaStore服务的地址
            HiveMetaStoreClient client = HiveMetaStoreSource.getClient();
            List<String> databases = client.getDatabases("*");
            for (String database : databases) {
                TableInfo tableInfo = new TableInfo();
                List<String> tables = client.getTables(database, "*");
                tableInfo.database = database;
                tables.forEach(x -> {
                    tableInfo.table = x;
                    tbls.add(tableInfo);
                });
            }
        } catch (TException e) {
            e.printStackTrace();
        }
        return tbls;
    }
}
